package hot100;

public class maxProduct {
    public int maxProduct(int[] nums) {
        if(nums.length <= 0) {
            return nums[0];
        }
        int n = nums.length;
        // dpup 以i为尾的所有子数组的最大乘积正数
        int[] dpup = new int[n];
        // dpdown 以i为尾的所有子数组的最小乘积负数
        int[] dpdown = new int[n];
        dpup[0] = nums[0];
        dpdown[0] = nums[0];
        int max =nums[0];
        for(int i = 1;i < n;i++) {
            if(nums[i] >= 0) {
                dpup[i] = Math.max(nums[i],nums[i] * dpup[i - 1]);
                dpdown[i] = nums[i] * dpdown[i - 1];
                max = Math.max(max,dpup[i]);
            }else {
                dpup[i] = nums[i] * dpdown[i - 1];
                dpdown[i] = Math.min(nums[i],nums[i] * dpup[ i - 1]);
                max = Math.max(max,dpup[i]);
            }
        }
        return max;

    }
}
